﻿using BZ.Core.Frm;
using NDK.Motion.MachineManager;
using LightController;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Frm_LightRecipe : Form
    {
        private Frm_LightRecipe()
        {
            InitializeComponent();
        }
        private static Frm_LightRecipe _instance;
        private static readonly object singletonLock = new object();
        public static Frm_LightRecipe Instance
        {
            get
            {
                if (_instance == null || _instance.IsDisposed)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null || _instance.IsDisposed)
                        {
                            _instance = new Frm_LightRecipe();
                        }
                    }
                }
                return _instance;
            }
        }

        private void Frm_LightRecipe_Load(object sender, EventArgs e)
        {
            ContextMenuItemInit();

            ReloadProImageLights();

            dlg_LightValueControl = Dlg_LightValueControl.Instance;
            dlg_LightValueControl.TopLevel = false;
            dlg_LightValueControl.Dock = DockStyle.Fill;
            pnl_LightValue.Controls.Add(dlg_LightValueControl);
            dlg_LightValueControl.Show();
        }



        private void ContextMenuItemInit()
        {
            // 
            // tsmi_AddProject
            // 
            this.tsmi_AddProject = new ToolStripMenuItem();
            this.tsmi_AddProject.Name = "tsmi_AddProject";
            this.tsmi_AddProject.Size = new System.Drawing.Size(160, 22);
            this.tsmi_AddProject.Text = "添加新项目";
            this.tsmi_AddProject.Click += new System.EventHandler(this.tsmi_AddProject_Click);
            // 
            // tsmi_CopyProject
            // 
            this.tsmi_CopyProject = new ToolStripMenuItem();
            this.tsmi_CopyProject.Name = "tsmi_CopyProject";
            this.tsmi_CopyProject.Size = new System.Drawing.Size(160, 22);
            this.tsmi_CopyProject.Text = "复制项目";
            this.tsmi_CopyProject.Click += new System.EventHandler(this.tsmi_CopyProject_Click);
            // 
            // tsmi_DeleteProject
            // 
            this.tsmi_DeleteProject = new ToolStripMenuItem();
            this.tsmi_DeleteProject.Name = "tsmi_DeleteProject";
            this.tsmi_DeleteProject.Size = new System.Drawing.Size(160, 22);
            this.tsmi_DeleteProject.Text = "删除项目";
            this.tsmi_DeleteProject.Click += new System.EventHandler(this.tsmi_DeleteProject_Click);
            // 
            // tsmi_AddLightController
            // 
            this.tsmi_AddImageLights = new ToolStripMenuItem();
            this.tsmi_AddImageLights.Name = "tsmi_AddImageLights";
            this.tsmi_AddImageLights.Size = new System.Drawing.Size(160, 22);
            this.tsmi_AddImageLights.Text = "添加图片光源组";
            this.tsmi_AddImageLights.Click += new System.EventHandler(this.tsmi_AddImageLights_Click);

            // 
            // tsmi_AddLightController
            // 
            this.tsmi_DeleteImageLights = new ToolStripMenuItem();
            this.tsmi_DeleteImageLights.Name = "tsmi_DeleteImageLights";
            this.tsmi_DeleteImageLights.Size = new System.Drawing.Size(160, 22);
            this.tsmi_DeleteImageLights.Text = "删除图片光源组";
            this.tsmi_DeleteImageLights.Click += new System.EventHandler(this.tsmi_DeleteImageLights_Click);

            this.cms_tvImageLights = new ContextMenuStrip();
            this.cms_tvImageLights.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.tsmi_AddImageLights});
            this.cms_tvImageLights.Name = "cms_tvImageLights";
            this.cms_tvImageLights.Size = new System.Drawing.Size(153, 70);

            // 
            // tsmi_AddLight
            // 
            this.tsmi_AddLight = new ToolStripMenuItem();
            this.tsmi_AddLight.Name = "tsmi_AddLight";
            this.tsmi_AddLight.Size = new System.Drawing.Size(160, 22);
            this.tsmi_AddLight.Text = "添加光源";
            this.tsmi_AddLight.Click += new System.EventHandler(this.tsmi_AddLight_Click);
            // 
            // tsmi_DeleteLight
            // 
            this.tsmi_DeleteLight = new ToolStripMenuItem();
            this.tsmi_DeleteLight.Name = "tsmi_DeleteLight";
            this.tsmi_DeleteLight.Size = new System.Drawing.Size(160, 22);
            this.tsmi_DeleteLight.Text = "删除光源";
            this.tsmi_DeleteLight.Click += new System.EventHandler(this.tsmi_DeleteLight_Click);

            this.cms_Project = new ContextMenuStrip();
            this.cms_Project.Name = "cms_Project";
            this.cms_Project.Size = new System.Drawing.Size(153, 70);
            cms_Project.Items.Add(tsmi_AddProject);
            cms_Project.Items.Add(tsmi_CopyProject);
            cms_Project.Items.Add(tsmi_DeleteProject);
            cms_Project.Items.Add(tsmi_AddImageLights);

            this.cms_Image = new ContextMenuStrip();
            this.cms_Image.Name = "cms_ImageLights";
            this.cms_Image.Size = new System.Drawing.Size(153, 70);
            cms_Image.Items.Add(tsmi_DeleteImageLights);
            cms_Image.Items.Add(tsmi_AddLight);

            this.cms_Lights = new ContextMenuStrip();
            this.cms_Lights.Name = "cms_Lights";
            this.cms_Lights.Size = new System.Drawing.Size(153, 70);
            cms_Lights.Items.Add(tsmi_DeleteLight);


        }

        private void ReloadProImageLights()
        {
            //LightController
            if (MachineDataManager.Instance.ProjectImageLights != null)
            {
                tv_ProImgLightList.Nodes.Clear();
                tv_ProImgLightList.ContextMenuStrip = cms_tvImageLights;
                foreach (var projectImageLights in MachineDataManager.Instance.ProjectImageLights)
                {
                    string nodeName = projectImageLights.Name;
                    var tn_Pro = new TreeNode(nodeName);
                    tn_Pro.Name = nodeName;
                    tn_Pro.Text = nodeName;
                    tn_Pro.Tag = projectImageLights;
                    tn_Pro.ContextMenuStrip = cms_Project;
                    tv_ProImgLightList.Nodes.Add(tn_Pro);
                    foreach (var imageLights in projectImageLights.ImageLightsList)
                    {
                        string nodeName_Img = imageLights.ImageName;
                        var tn_Lights = new TreeNode(nodeName);
                        tn_Lights.Name = nodeName_Img;
                        tn_Lights.Text = nodeName_Img;
                        tn_Lights.Tag = imageLights;
                        tn_Lights.ContextMenuStrip = cms_Lights;
                        tn_Pro.Nodes.Add(tn_Lights);
                        foreach (var light in imageLights.LightList)
                        {
                            if (light == null)
                                continue;
                            var tn1 = new TreeNode(light.Name);
                            tn1.Name = light.Name;
                            tn1.Text = light.Name;
                            tn1.Tag = light;
                            tn_Lights.Nodes.Add(tn1);
                        }
                    }
                }
                tv_ProImgLightList.ExpandAll();
            }
        }

        private void tv_ProImgLightList_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            if (e.Node?.Tag is ProjectImageLights)
            {
                try
                {
                    //获取编辑的节点名称
                    string oldName = tv_ProImgLightList.SelectedNode.Text;

                    //获取修改后的新名称,没有修改或则名称为空字符串则返回
                    if (e.Label == string.Empty || e.Label == null)
                    {
                        if (!string.IsNullOrEmpty(oldName))
                        {
                            e.Node.Text = oldName;
                            e.CancelEdit = true;
                            return;
                        }
                    }
                    string newName = e.Label != "NULL" ? e.Label : oldName;

                    ProjectImageLights node = (ProjectImageLights)(e.Node.Tag);
                    ProjectImageLights pro = MachineDataManager.Instance.ProjectImageLights.Find(c => c.Name == node.Name);
                    if (pro.Name != newName)
                    {
                        pro.Name = newName;
                    }
                    MachineDataManager.SaveToFile();
                }
                catch (Exception ex)
                {

                }
            }
        }

        private void tv_ImageLights_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.Node?.Tag is ProjectImageLights)
                {
                    e.Node.ContextMenuStrip = cms_Project;
                }
                else if (e.Node?.Tag is ImageLights)
                {
                    e.Node.ContextMenuStrip = cms_Image;
                }
                else if (e.Node?.Tag is Light)
                {
                    e.Node.ContextMenuStrip = cms_Lights;
                }

            }
            else if (e.Button == MouseButtons.Left)
            {
                if (e.Node?.Tag is ProjectImageLights)
                {
                    tv_ProImgLightList.LabelEdit = true;
                    ProjectImageLights node = (ProjectImageLights)e.Node?.Tag;
                    if (node == null)
                    {
                        return;
                    }
                    if (node.ImageLightsList.Count == 0)
                    {
                        return;
                    }
                }
                else
                {
                    tv_ProImgLightList.LabelEdit = false;
                    if (e.Node?.Tag is ImageLights)
                    {
                        ImageLights imageLights = (ImageLights)e.Node?.Tag;
                        if (imageLights?.LightList == null)
                        {
                            return;
                        }
                        if (imageLights.LightList.Count == 0)
                        {
                            return;
                        }
                    }
                    else if (e.Node?.Tag is Light)
                    {
                        var light = (Light)(e.Node?.Tag);
                        ImageLights imageLights = (ImageLights)e.Node?.Parent?.Tag;
                        if (imageLights?.LightList == null)
                        {
                            return;
                        }
                        if (imageLights.LightList.Count == 0)
                        {
                            return;
                        }
                        dlg_LightValueControl.Light = light;

                    }
                }

            }
        }

        private void tv_ProImgLightList_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (e.Node?.Tag is ImageLights)
                {
                    ImageLights node = (ImageLights)e.Node?.Tag;
                    if (node == null)
                    {
                        return;
                    }
                    //关闭其他光源
                    foreach (var lightController in MachineDataManager.Instance.LightControllerList)
                    {
                        lightController.CloseAllLight();
                    }
                    node.Open();
                }
                else if (e.Node?.Tag is Light)
                {
                    Light node = (Light)e.Node?.Tag;
                    if (node == null)
                    {
                        return;
                    }
                    //关闭其他光源
                    foreach (var lightController in MachineDataManager.Instance.LightControllerList)
                    {
                        lightController.CloseAllLight();
                    }
                    node.ON();
                }
            }
        }

        private bool CreatProjectOnlyName(out string ProjectName)
        {
            ProjectName = "";
            var ProjectNameInit = "未定义";
            int MaxCount = 20;
            for (int i = 0; i < MaxCount; i++)
            {
                var Name = $"{ProjectNameInit}{i}";
                var findItem = MachineDataManager.Instance.ProjectImageLights.Find(x => x.Name.Equals(Name));
                if (findItem != null)
                {
                    if (i == MaxCount - 1)
                        return false;
                }
                else
                {
                    ProjectName = Name;
                    return true;
                }
            }
            return false;
        }

        private void tsmi_AddProject_Click(object sender, EventArgs e)
        {
            var ProjectName = "";
            var ret = CreatProjectOnlyName(out ProjectName);
            if (ret)
            {
                MachineDataManager.Instance.ProjectImageLights.Add(new ProjectImageLights(ProjectName, new List<ImageLights>()));
                ReloadProImageLights();
            }
            else
            {
                MessageBox.Show("未定义项目过多,请修改项目名称");
                return;
            }
        }
        private void tsmi_CopyProject_Click(object sender, EventArgs e)
        {
            if (tv_ProImgLightList.SelectedNode?.Tag is ProjectImageLights)
            {
                ProjectImageLights NodeTag = (ProjectImageLights)tv_ProImgLightList.SelectedNode?.Tag;
                if (NodeTag == null)
                {
                    return;
                }
                ProjectImageLights projectImageLights = MachineDataManager.Instance.ProjectImageLights.Find(l => l.Name == NodeTag.Name);
                if (projectImageLights == null)
                {
                    return;
                }
                var ProjectName = "";
                var ret = CreatProjectOnlyName(out ProjectName);
                if (ret)
                {
                    MachineDataManager.Instance.ProjectImageLights.Add(new ProjectImageLights(ProjectName, projectImageLights.ImageLightsList));
                    MachineDataManager.SaveToFile();
                    ReloadProImageLights();
                }
                else
                {
                    MessageBox.Show("未定义项目过多,请修改项目名称");
                    return;
                }
            }
        }

        private void tsmi_DeleteProject_Click(object sender, EventArgs e)
        {
            if (tv_ProImgLightList.SelectedNode?.Tag is ProjectImageLights)
            {
                ProjectImageLights NodeTag = (ProjectImageLights)tv_ProImgLightList.SelectedNode?.Tag;
                if (NodeTag == null)
                {
                    return;
                }
                ProjectImageLights projectImageLights = MachineDataManager.Instance.ProjectImageLights.Find(l => l.Name == NodeTag.Name);
                if (projectImageLights == null)
                {
                    return;
                }
                Dlg_Confirm dlg_Confirm = new Dlg_Confirm("确认框", $"确定删除{projectImageLights.Name}");
                if (dlg_Confirm.ShowDialog(this) == DialogResult.OK)
                {
                    MachineDataManager.Instance.ProjectImageLights.Remove(projectImageLights);
                    MachineDataManager.SaveToFile();
                    ReloadProImageLights();
                }
            }
        }
        /// <summary>
        /// 添加图片光源组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_AddImageLights_Click(object sender, EventArgs e)
        {
            if (tv_ProImgLightList.SelectedNode?.Tag is ProjectImageLights)
            {
                ProjectImageLights NodeTag = (ProjectImageLights)tv_ProImgLightList.SelectedNode?.Tag;
                if (NodeTag == null)
                {
                    return;
                }
                Dlg_AddImageLights addImageLights = new Dlg_AddImageLights();
                if (addImageLights.ShowDialog(this) == DialogResult.OK)
                {
                    ImageLights imageLights = addImageLights.ImageLights;

                    var t = NodeTag.ImageLightsList.FindAll(l => l.ImageName == imageLights.ImageName);
                    if (t == null || t.Count == 0)//没有相同名字的才能加入
                    {
                        NodeTag.ImageLightsList.Add(imageLights);
                    }
                    else
                    {
                        MessageBox.Show(imageLights.ImageName + "已经存在!");
                        return;
                    }

                    ReloadProImageLights();
                }
            }

        }

        /// <summary>
        /// 删除图片光源组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_DeleteImageLights_Click(object sender, EventArgs e)
        {
            if (tv_ProImgLightList.SelectedNode?.Tag is ImageLights)
            {
                ImageLights imageLight = (ImageLights)tv_ProImgLightList.SelectedNode?.Tag;
                if (imageLight == null)
                {
                    return;
                }
                ProjectImageLights project = (ProjectImageLights)tv_ProImgLightList.SelectedNode?.Parent?.Tag;
                if (project == null)
                {
                    return;
                }

                Dlg_Confirm dlg_Confirm = new Dlg_Confirm("确认框", $"确定删除{imageLight.ImageName}");
                if (dlg_Confirm.ShowDialog(this) == DialogResult.OK)
                {
                    project.ImageLightsList.Remove(imageLight);
                    MachineDataManager.SaveToFile();
                    ReloadProImageLights();
                }
            }
        }

        /// <summary>
        /// 添加光源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_AddLight_Click(object sender, EventArgs e)
        {
            if (tv_ProImgLightList.SelectedNode == null)
                return;
            ImageLights imageLights = tv_ProImgLightList.SelectedNode.Tag as ImageLights;

            Dlg_AddLight dlg_AddLight = new Dlg_AddLight();
            dlg_AddLight.StartPosition = FormStartPosition.CenterScreen;
            var res = dlg_AddLight.ShowDialog();
            if (res == DialogResult.OK)
            {
                dlg_AddLight.Light.Type = imageLights.ImageName;
                imageLights.LightList.Add(dlg_AddLight.Light);
                //dgv_LightValue.DataSource = new BindingList<Light>(MachineDataManager.Instance.LightList);
                ReloadProImageLights();
            }

        }

        /// <summary>
        /// 删除光源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmi_DeleteLight_Click(object sender, EventArgs e)
        {
            TreeNode tn = tv_ProImgLightList.SelectedNode;
            Light LightTag = (Light)tv_ProImgLightList.SelectedNode?.Tag;
            if (tn == null && LightTag != null)
            {
                return;
            }
            ImageLights imageLightsTag = (ImageLights)tn.Parent.Tag;
            if (imageLightsTag == null)
            {
                return;
            }
           
            Dlg_Confirm dlg_Confirm = new Dlg_Confirm("确认框", $"确定删除{LightTag.Name}");
            if (dlg_Confirm.ShowDialog(this) == DialogResult.OK)
            {
                imageLightsTag.LightList.Remove(LightTag);
                MachineDataManager.SaveToFile();
                ReloadProImageLights();
            }
        }

        private System.Windows.Forms.ContextMenuStrip cms_tvImageLights;
        private System.Windows.Forms.ToolStripMenuItem tsmi_AddProject;
        private System.Windows.Forms.ToolStripMenuItem tsmi_CopyProject;
        private System.Windows.Forms.ToolStripMenuItem tsmi_DeleteProject;
        private System.Windows.Forms.ToolStripMenuItem tsmi_AddImageLights;
        private System.Windows.Forms.ToolStripMenuItem tsmi_DeleteImageLights;
        private System.Windows.Forms.ToolStripMenuItem tsmi_AddLight;
        private System.Windows.Forms.ToolStripMenuItem tsmi_DeleteLight;

        private System.Windows.Forms.ContextMenuStrip cms_Project;
        private System.Windows.Forms.ContextMenuStrip cms_Image;
        private System.Windows.Forms.ContextMenuStrip cms_Lights;

        Dlg_LightValueControl dlg_LightValueControl;


    }
}
